package com.lesports.qmt.web.service;

import com.lesports.api.common.CallerParam;
import com.lesports.qmt.sbc.api.dto.TNews;
import com.lesports.qmt.sbc.api.dto.TTopicItem;
import com.lesports.qmt.sbc.api.dto.TTopicItemPackage;
import com.lesports.qmt.sbc.client.QmtSbcApis;
import com.lesports.qmt.web.model.SubjectContentVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017-01-19.
 */
@Service("topicsService")
public class TopicsService {
    private static final Logger LOG = LoggerFactory.getLogger(TopListService.class);

    public List<SubjectContentVo> listItems(long packageId, CallerParam caller) {
        List<SubjectContentVo> list = new ArrayList<SubjectContentVo>();
        SubjectContentVo entity = null;
        TTopicItemPackage model = QmtSbcApis.getTTopicItemPackageById(packageId, caller);
        if (model == null) {
            LOG.warn("topic package not exists: id:{}", packageId);
            return null;
        } else {
            if (model.getItems() != null && model.getItems().size() > 0) {
                for (TTopicItem item : model.getItems()) {
                    entity = new SubjectContentVo();
                    entity.setItemId(item.getItemId());
                    entity.setName(item.getName());
                    entity.setOrder(item.getOrder());
                    entity.setOriginalName(item.getOriginalName());
                    entity.setSubName(item.getSubName());
                    entity.setType(item.getType());
                    TNews ne = QmtSbcApis.getTNewsById(item.getItemId(), caller);
                    if (ne != null) {
                        entity.setContent(ne.getContent());
                    }
                    list.add(entity);
                }
            }
        }
        return list;
    }
}